/*
 * Main.java
 *
 * Created on 23. duben 2008, 21:16
 */
package net.slavics.maps;

import net.slavics.maps.PanelMap;
import java.awt.Dimension;
import java.awt.Point;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JFrame;
import net.slavics.maps.ifc.IMapa;
import net.slavics.maps.ifc.MapComponent;

/**
 *
 * @author  Petr Slavíček, Dis. <pslavicek@seznam.cz>
 */
public class Main extends JFrame implements Runnable, MapComponent {

    static final long serialVersionUID = 451321315L;
    private IMapa iMapa;

    /** Creates new form Main */
    public Main() {
        initComponents();
        setSize(new Dimension(600, 400));
        iMapa = new net.maps.OldMaps();
        iMapa.setOwner(this);
        jSplitPane1.setRightComponent(new PanelMap(this, iMapa, new Point(jSplitPane1.getWidth(), jSplitPane1.getHeight())));
        //(new Thread(this)).start();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        mainProgressBar = new javax.swing.JProgressBar();
        jLabelXY = new javax.swing.JLabel();
        jMenuBar2 = new javax.swing.JMenuBar();
        jMenu3 = new javax.swing.JMenu();
        jMenu4 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("MapsViewer");

        jSplitPane1.setDividerSize(3);

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        mainProgressBar.setStringPainted(true);

        jLabelXY.setText("Souřadnice");

        jMenu3.setText("File");
        jMenuBar2.add(jMenu3);

        jMenu4.setText("Edit");
        jMenuBar2.add(jMenu4);

        setJMenuBar(jMenuBar2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(mainProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 337, Short.MAX_VALUE)
                .addComponent(jLabelXY)
                .addContainerGap())
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 585, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelXY)
                    .addComponent(mainProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Main().setVisible(true);
            }
        });
    }

    public Proxy getProxy() {
        return null;
        //return new Proxy(Proxy.Type.HTTP, new InetSocketAddress("cache-bak.pre.cz", 8080));
    }

    /**
     * Zobrazí souřadnice
     * @param p
     * @param shift posun
     */
    public void setLabelSouradnice(Point p, Point shift) {
        Point tr = iMapa.getGeoTransfer().transfer(new Point(p.x + shift.x, p.y + shift.y));
        jLabelXY.setText(String.format("%s Souřadnice: %,d,%,d", iMapa.getName(), tr.x, tr.y));
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabelXY;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuBar jMenuBar2;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JProgressBar mainProgressBar;
    // End of variables declaration//GEN-END:variables
    public void run() {
        while (true) {
            for (int i = 0; i < 100; i++) {
                try {
                    mainProgressBar.setValue(i);
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
    Map<String, Integer> progress = new HashMap<String, Integer>();
    private void reCalcProgressBar(){
        //mainProgressBar.setMaximum(progress.size()*100);
        int podil=0;
        for (Integer i : progress.values()) {
            podil+=i;
        }
        mainProgressBar.setValue(podil);
    }
    /**
     * {@inheritDoc}
     * @param name
     * @param procenta
     */
    public void addProgressBar(String name, int procenta) {
        progress.put(name, procenta);
        reCalcProgressBar();
    }
    /**
     * {@inheritDoc}
     * @param name
     */
    public void removeProgressBar(String name) {
        progress.remove(name);
        reCalcProgressBar();
    }
}
